<template>
  <!-- 对话框表单 -->
  <el-dialog class="ba-operate-dialog"
             :close-on-click-modal="false"
             :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
             @close="baTable.toggleForm"
             :destroy-on-close="true">
    <template #header>
      <div class="title"
           v-drag="['.ba-operate-dialog', '.el-dialog__header']"
           v-zoom="'.ba-operate-dialog'">
        {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
      </div>
    </template>
    <el-scrollbar v-loading="baTable.form.loading"
                  class="ba-table-form-scrollbar">
      <div class="ba-operate-form"
           :class="'ba-' + baTable.form.operate + '-form'"
           :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
        <el-form ref="formRef"
                 @keyup.enter="baTable.onSubmit(formRef)"
                 :model="baTable.form.items"
                 :label-position="config.layout.shrink ? 'top' : 'right'"
                 :label-width="baTable.form.labelWidth + 'px'"
                 v-if="!baTable.form.loading">
          <el-form-item label="状态">
            <el-select class="w100"
                       v-model="baTable.form.items!.status"
                       :placeholder="t('Please input field', { field: '状态' })"
                       clearable>
              <el-option :label="v.label"
                         :value="v.value"
                         v-for="v in statusList"
                         :key="v.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="运营">
            <el-select class="w100"
                       multiple
                       v-model="baTable.form.items!.user_id"
                       :placeholder="t('Please input field', { field: '运营' })"
                       clearable>
              <el-option :label="v.nickname"
                         :value="v.id"
                         v-for="v in userList"
                         :key="v.id"></el-option>
            </el-select>
          </el-form-item>
          <FormItem label="开始时间"
                    v-model="baTable.form.items!.start_time"
                    type="datetime"
                    prop="start_time"
                    :placeholder="t('Please input field', { field: '开始时间' })" />
        </el-form>
      </div>
    </el-scrollbar>
    <template #footer>
      <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
        <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
        <el-button v-blur
                   :loading="baTable.form.submitLoading"
                   @click="baTable.onSubmit(formRef)"
                   type="primary">
          {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, inject, watch, onBeforeMount} from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import type { FormInstance, FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { useAdminInfo } from '/@/stores/adminInfo'
import { useConfig } from '/@/stores/config'
import { getOperationsList }  from '/@/api/backend/common'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass
const userList = ref([])
const statusList = ref([
    {
        label: '正常',
        value: 'normal'
    },
    {
        label: '冻结',
        value: 'hidden'
    }
])
const { t } = useI18n()
// const rules: Partial<Record<string, FormItemRule[]>> = reactive({
//     status: [buildValidatorData({ name: 'required', title: '状态' })],
//     user_id: [buildValidatorData({ name: 'required', title: '运营' })],
//     start_time: [buildValidatorData({ name: 'required', title: '开始时间' })],
// })

watch(
    () => baTable.form.operate,
    (newVal) => {
       if(newVal) getOpList()
    }
)

const getOpList = () => {
    getOperationsList().then(res => {
        if(res.data && res.data.list.length) {
            res.data.list.forEach((v:any) => {
                v.id = String(v.id)
            })
            userList.value = res.data.list
        }
    })
}

</script>